import numpy

def signal_derivative_1D(signal, dx):
    """
    Calculate the derivative of a 1-D signal
        Endpoints have a one sided derivative.
    """
    if type(signal) == type([]):
        signal = numpy.array(signal)
    before = cshift(signal, 1)
    after = cshift(signal, -1)
    derivative = after - before
    derivative /= 2*dx
    # now fix the first and last derivative
    derivative[0] = (signal[1] - signal[0]) / dx
    derivative[-1] = (signal[-1] - signal[-2]) / dx
    return derivative

def cshift(signal, offset):
    '''
    will return an array which is the signal array that has been circularly shifted by
        offset indexes.
    '''
    offset %= len(signal)
    return numpy.concatenate( (signal[-offset:], signal[:-offset]) )
    
